package com.example.student.service.impl;

import com.example.student.mapper.ClassMapper;
import com.example.student.pojo.Classes;
import com.example.student.pojo.PageBean;
import com.example.student.pojo.Result;
import com.example.student.pojo.Stu;
import com.example.student.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public void delete(Integer id) {
        classMapper.deleteByClassId(id);
    }

    @Override
    public PageBean pages(Integer page, Integer pageSize) {
        System.out.println(page);
        System.out.println(pageSize);

        long count=classMapper.count();
        Integer start =(page-1)*pageSize;
        List<Classes> list=classMapper.page(start,pageSize);
        PageBean pageBean=new PageBean(count,list);
        return pageBean;
    }

    @Override
    public Result add(Classes cla) {
        cla.setCreateTime(LocalDateTime.now());
        cla.setUpdateTime(LocalDateTime.now());
        if (findById(cla.getDeptId()) > 0) {
            return Result.error("该班级ID 已被使用，请使用其他 ID。");
        }
        cla.setCreateTime(LocalDateTime.now());
        cla.setUpdateTime(LocalDateTime.now());

        classMapper.insert(cla);
        return Result.success();
    }

    public int findById(Integer classId) {
        return classMapper.findById(classId);
    }

}
